<?php

/**
 * This is the model class for table "tests_question".
 *
 * The followings are the available columns in table 'tests_question':
 * @property integer $tests_question_id
 * @property string $tests_question_title
 * @property integer $tests_question_type
 * @property string $tests_question_content
 * @property string $tests_question_answer
 * @property string $tests_question_explanation
 * @property integer $tests_question_qd
 * @property integer $tests_question_tid
 * @property integer $tests_question_sid
 * @property integer $tests_question_score
 * @property string $tests_question_create_time
 * @property string $tests_question_edit_time
 */
class TestsQuestion extends CActiveRecord
{
	//试卷的答案，为生成列表提供
	public $answer;
	/**
	 * Returns the static model of the specified AR class.
	 * @return TestsQuestion the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tests_question';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tests_question_title', 'length', 'max'=>255),
			array('tests_question_title,tests_question_score','required'),
			array('tests_question_score','numerical','max'=>'100','min'=>'0'),
			//目前缺少一个判断就是试卷分数之和不能大于100
			array('tests_question_title, tests_question_type, tests_question_content, tests_question_answer, tests_question_explanation, tests_question_qd, tests_question_tid, tests_question_sid,tests_question_score', 'safe'),

			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('tests_question_id, tests_question_title, tests_question_type, tests_question_content, tests_question_answer, tests_question_explanation, tests_question_qd, tests_question_tid, tests_question_sid, tests_question_score, tests_question_create_time, tests_question_edit_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'QID'=>array(self::BELONGS_TO,'Quesetions','tests_question_qd'),
			'TID'=>array(self::BELONGS_TO,'Tests','tests_question_tid'),
			'SID'=>array(self::BELONGS_TO,'Subjects','tests_question_sid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'tests_question_id' => '试卷试题管理',
			'tests_question_title' => '试卷试题题目',
			'tests_question_type' => '试卷试题题型',
			'tests_question_content' => '试卷试题内容',
			'tests_question_answer' => '试卷试题答案',
			'tests_question_explanation' => '试卷试题解析',
			'tests_question_qd' => '试题ID',
			'tests_question_tid' => '试卷ID',
			'tests_question_sid' => '大题ID',
			'tests_question_score' => '占分比',
			'tests_question_create_time' => '创建时间',
			'tests_question_edit_time' => '修改时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('tests_question_id',$this->tests_question_id);

		$criteria->compare('tests_question_title',$this->tests_question_title,true);

		$criteria->compare('tests_question_type',$this->tests_question_type);

		$criteria->compare('tests_question_content',$this->tests_question_content,true);

		$criteria->compare('tests_question_answer',$this->tests_question_answer,true);

		$criteria->compare('tests_question_explanation',$this->tests_question_explanation,true);

		$criteria->compare('tests_question_qd',$this->tests_question_qd);

		$criteria->compare('tests_question_tid',$this->tests_question_tid);

		$criteria->compare('tests_question_sid',$this->tests_question_sid);

		$criteria->compare('tests_question_score',$this->tests_question_score);

		$criteria->compare('tests_question_create_time',$this->tests_question_create_time,true);

		$criteria->compare('tests_question_edit_time',$this->tests_question_edit_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function beforeSave() {
		if(!$this->tests_question_score){
			$this->tests_question_score = '0';
		}
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->tests_question_create_time = $this->tests_question_edit_time = date("Y-m-d H:i:s");
			}else{
				$this->tests_question_edit_time = date("Y-m-d H:i:s");
			}
			return true;
		}else{
			return false;
		};
	}
	/**
	 * 判分：如果正确则返回占分比数值，如果错误则返回0
	 * @param unknown_type $id 试卷试题ID
	 * @param unknown_type $answer 答卷答案
	 */
	public function getScore($id,$answer) {
		$rs = $this->findByPk($id);
		return ($answer == $rs->tests_question_answer)?$rs->tests_question_score:'0';
	}
}